package com.fyx.controller;

import com.fyx.annotation.Log;
import com.fyx.entity.WechatInfo;
import com.fyx.entity.WxRequest;
import com.fyx.modules.system.domain.WxButton;
import com.fyx.service.IWeChatService;
import com.fyx.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Api(tags = "微信公众号")
@RestController
@RequestMapping("/api/weChat")
@RequiredArgsConstructor
public class WeChatController {
    @Autowired
    private IWeChatService iweChatService;

    @Log("查询公众号回复")
    @ApiOperation("查询公众号回复")
    @GetMapping(value = "getUserUnionid")
    @ResponseBody
    public String getUserUnionid(@Validated String unionid){
          iweChatService.insertWxUser(unionid);
        return "OK";
    }


    @Log("查询公众号回复")
    @ApiOperation("查询公众号回复")
    @GetMapping(value = "wxRequest")
    @PreAuthorize("@el.check('wxRequest:list')")
    public ResponseEntity<Object> query(WxRequest wxRequest, Pageable pageable){
        Map mapResult = iweChatService.wxRequest(wxRequest,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("新增公众号回复")
    @ApiOperation("新增公众号回复")
    @PostMapping(value="addWxRequest")
    @PreAuthorize("@el.check('wxRequest:add')")
    public ResponseEntity<Object> addWxRequest(@Validated @RequestBody WxRequest wxRequest){
        iweChatService.addWxRequest(wxRequest);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改公众号回复")
    @ApiOperation("修改公众号回复")
    @PutMapping(value = "updateWxRequest")
    @PreAuthorize("@el.check('wxRequest:edit')")
    public ResponseEntity<Object> updateWxRequest(@Validated(WxButton.Update.class) @RequestBody WxRequest wxRequest){
        iweChatService.updateWxRequest(wxRequest);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除公众号回复")
    @ApiOperation("删除公众号回复")
    @DeleteMapping(value = "deleteWxRequest")
    @PreAuthorize("@el.check('wxRequest:del')")
    public ResponseEntity<Object> deleteWxRequest(@RequestBody String[] ids){
        iweChatService.deleteWxRequest(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("查询公众号接口")
    @ApiOperation("查询公众号接口")
    @GetMapping(value = "officialAccounts")
    @PreAuthorize("@el.check('officialAccounts:list')")
    public ResponseEntity<Object> officialAccounts(WechatInfo wechatInfo, Pageable pageable){
        Map mapResult = iweChatService.officialAccounts(wechatInfo,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("新增公众号接口")
    @ApiOperation("新增公众号接口")
    @PostMapping(value = "addOfficialAccounts")
    @PreAuthorize("@el.check('officialAccounts:add')")
    public ResponseEntity<Object> addOfficialAccounts(@Validated @RequestBody WechatInfo wechatInfo){
        iweChatService.addOfficialAccounts(wechatInfo);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改公众号接口")
    @ApiOperation("修改公众号接口")
    @PutMapping(value = "updateOfficialAccounts")
    @PreAuthorize("@el.check('officialAccounts:edit')")
    public ResponseEntity<Object> updateOfficialAccounts(@Validated(WxButton.Update.class) @RequestBody WechatInfo wechatInfo){
        iweChatService.updateOfficialAccounts(wechatInfo);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除公众号接口")
    @ApiOperation("删除公众号接口")
    @DeleteMapping(value = "deleteOfficialAccounts")
    @PreAuthorize("@el.check('officialAccounts:del')")
    public ResponseEntity<Object> deleteOfficialAccounts(@RequestBody String[] ids){
        iweChatService.deleteOfficialAccounts(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
